angular.module('module.advertisement').controller('AdDemurController', AdDemurController);

AdDemurController.$inject = ['$state', '$stateParams', '$localStorage', 'AdvertisementService'];

function AdDemurController($state, $stateParams, $localStorage, AdvertisementService) {

    var vm = this;
    vm.adid = $stateParams.adid;
    vm.adInfo = {};
    vm.shopInfo = {};
    vm.unusualInfo = {};
    vm.shopInfo.adid = vm.adid;
    vm.shopInfo.state = 0;
    active(vm.adid);
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    vm.submit = submit;

    // 设置提示模态框的显示隐藏状态
    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }

    var shopid = $localStorage[environmental + "shopid"];

    //////////////////////////////////////////////////

    function active(adid) {
        AdvertisementService.advert(adid).$promise.then(function (advert) {
            vm.adInfo.img = advert.img;
            vm.adInfo.name = advert.name;
            vm.adInfo.type = advert.adverttype;
            vm.adInfo.qrcode = advert.qrcode;
        });

        AdvertisementService.getById(adid).$promise.then(function (data) {
            vm.adInfo.starttime = data[0].starttime;
            vm.adInfo.endtime = data[0].endtime;
            vm.adInfo.orderid = data[0].orderid;
        });
    }

    vm.shopInfo.shopid = shopid;
    function submit() {
        if (!vm.reason) {
            vm.messageVisible = true;
            vm.message = "请填写您遇到的问题！";
            return;
        }
        vm.shopInfo.reason = vm.reason;
        $('.addmur_btn_submit').attr('disabled', 'disabled');
        AdvertisementService.shopUnusual(vm.shopInfo).$promise.then(function (info) {
            vm.advertInfo = {
                adid: info.unusualid,
                astate: 9,
                txt: "替代异常广告",
                name: "小灰灰",
                adverttype: "替代异常广告",
                img: "http://inner.test.shuwang.info/common/img/1030/2017-11-14/6uxtXXHuT-timg9.jpg",
                qrcode: "https://www.baidu.com",
                qrdetail: "{\"mode\":0,\"info\":\"https://www.baidu.com\"}",
                crc: "DB280F0",
                advertiserid: 1483067064109
            };
            AdvertisementService.addAdvert(vm.advertInfo).$promise.then(function (data) {
                AdvertisementService.updateUnusual(vm.adid, vm.shopInfo.shopid, info.unusualid).$promise.then(function (unusual) {
                    vm.messageVisible = true;
                    vm.message = "已为你替换广告！";
                    $state.go("adlist")
                }, function (err) {
                    if (err.data.status) {
                        vm.messageVisible = true;
                        vm.message = "已为你替换广告，但是推送设备失败！！";
                        $state.go("adlist")
                    } else {
                        vm.messageVisible = true;
                        vm.message = "异常更新失败！";
                        $state.go("adlist")
                    }

                })
            })
        });
    }
}